<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
class CateController extends Controller
{
    //add
    public function add()
    {
    	//查询所有分类
    	// $data = DB::table('cate') -> get();
    	// dd($data);
    	
    	//排序sql语句
    	// SELECT id,name,path,CONCAT(path,',',id) AS sortPath FROM mll_cate ORDER BY sortPath ;
    	// 按照整型排序
    	// SELECT id,name,path,CONCAT(path,',',id) AS sortPath FROM mll_cate ORDER BY CAST('sortPath' AS SIGNED );
    	
    	$data = DB::table('cate')
    		-> select(['id', 'name', 'path', DB::raw('CONCAT(path, ",", id) AS sortPath')])
    		->orderBy('sortPath', 'ASC')
    		// -> orderBy(DB::raw('CAST("sortPath" AS SIGNED)'),'ASC')
    		-> get();

    		//dd($data);

    	foreach($data as $k => $v)
    	{
    		$count = substr_count($v -> path, ',');
    		$str = str_repeat('|----',$count);
    		$data[$k] -> name = $str.$v -> name;

    	}

    	// dd($data);
    	return view('admin.cate.add',['data' => $data]);
    }

    //insert
    public function insert(Request $request)
    {
    	// dd($request -> all());

    	$this -> validate($request,[
    		'name' => 'required|unique:cate'
    		],[
    		'name.required' => '分类名称不能为空',
    		'name.unique' => '分类名称不能重复'
    		]);


    	$data = $request -> except('_token');

    	if($request -> input('pid') == 0)
    	{
    		$data['path'] = 0;
    		// dd($data);
    	}else
    	{

    		$path = DB::table('cate') -> where('id',$request -> input('pid')) ->first() -> path;
	    	// dd($path); 
	    	
	    	// 处理path字段
	    	$data['path'] = $path.','.$request -> input('pid');
    		// dd($data);

    	}
    	
    	//添加数据
    	$res = DB::table('cate') -> insert($data);

    	//判断
    	if($res)
    	{
    		return redirect('/admin/cate/index') -> with(['info' => '添加成功']);
    	}else
    	{
    		return back() -> with(['error' => '添加失败']);
    	}
    	
    }

    //index
    public function index()
    {
    	//查询
    	// $data = DB::table('cate') -> get();

    	// SELECT c1.*,c2.name AS pname,CONCAT(c1.path,',',c1.id) AS sortPath FROM mll_cate AS c1 LEFT JOIN mll_cate AS c2 ON c1.pid = c2.id ORDER BY sortPath ASC;
    	
    	$data = DB::table('cate AS c1')
    		// -> select('c1.*', 'c2.name AS pname', DB::raw("CONCAT(c1.path,',',c1.id) AS sortPath"))
    		-> select('c1.*','c2.name AS pname', DB::raw("CONCAT(mll_c1.path,',',mll_c1.id) AS sortPath"))
    		-> leftJoin('cate AS c2', 'c1.pid', '=', 'c2.id')
    		-> orderBy('sortPath', 'ASC')
    		-> get();
    	// dd($data);
    	
    	//处理路径
    	foreach($data as $k => $v)
    	{
    		$count = substr_count($v -> path, ',');
    		$str = str_repeat('|----', $count);
    		$data[$k] -> pname = $str.$v -> pname;

    		if(empty($data[$k] -> pname))
    		{
    			$data[$k] -> pname = '根分类';
    		}
    	}
    	return view('admin.cate.index',['data' => $data]);
    }

    //edit
    public function edit($id)
    {
    	//查询当前分类
    	$data = DB::table('cate')
    	 	-> where('id',$id)
    	 	-> first();

    	 //查询所有分类
    	$allData = DB::table('cate')
    		-> select(['id', 'name', 'path', DB::raw('CONCAT(path,",",id) AS sortPath')])
    		-> orderBy('sortPath', 'ASC')
    		-> get();
    	foreach($allData as $k => $v)
    	{
    		$count = substr_count($v -> path, ',');
    		$str = str_repeat('|----', $count);
    		$allData[$k] -> name = $str.$v -> name;
    	}
    	return view('admin.cate.edit',['data' => $data, 'allData' => $allData]);
    }

    //update
    public function update(Request $request)
    {
    	$this -> validate($request,[
    		'name' => 'required'
    		],[
    		'name.required' => '分类名称不能为空'
    		]);

    	$data = $request -> except('_token');
    	// dd($data);


    	//判断是否是根分类
    	if($data['pid'] == 0)
    	{
    		$data['path'] = 0;
    	}else
    	{
    		//查询父path
	    	$parentPath = DB::table('cate')
	    		-> where('id',$data['pid'])
	    		-> first()
	    		-> path;

	    	// dd($parentPath);
	    	//拼接当前path
	    	$data['path'] = $parentPath.','.$data['pid'];
    	}
    	

    	//更新
    	$res = DB::table('cate')
    		-> where('id', $data['id'])
    		-> update($data);

    	//判断
    	if($res)
    	{
    		return redirect('/admin/cate/index') -> with(['info' => '更新成功']);
    	}else
    	{
    		return back() -> with(['error' => '更新失败']);
    	}
    }

    //delete
    public function delete($id)
    {
    	$res = DB::table('cate')
    		-> where('pid', $id)
    		-> get();
    	//判断
    	if($res)
    	{
    		return redirect('admin/cate/index') -> with(['error' => '有子类，不能删除']);
    	}else
    	{	
    		$res = DB::table('cate') -> delete($id);
    		if($res)
    		{
    			return back() -> with(['info' => '删除成功']);
    		}else
    		{
    			return back() -> with(['error' => '删除失败']);
    		}
    	}
    }
}


